const Index = {
	age: label => ({validator: (_, v) => {
		if( window.$fn.isEmpty(v) ) return Promise.resolve()
		if( v > 0 ){
			if( v > 125 ){
				return Promise.reject(label + '必须小于 125')
			}else{
				return Promise.resolve()
			}
		}else{
			return Promise.reject(label + '必须大于 0')
		}
	}}),
	// 数字编号
	num_no: (label, value=2) => ({ validator: (_, v) => {
		if( window.$fn.isEmpty(v) ) return Promise.resolve()
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			if(v.toString().length !== value) return Promise.reject(label + '须为 ' + value + ' 个字符' )
			return n < 0 ? Promise.reject(label + '不能为负数') : Promise.resolve()
		}
	}}),
	// 以字母开头的编码
	letter_no: (label, value=3) => ({ validator: (_, v) => {
		if( window.$fn.isEmpty(v) ) return Promise.resolve()
		if( !/[A-Z][0-9]+$/g.test(v) ) return Promise.reject(label + '是以大写字母开头的数字' )
		if(v.toString().length !== value) return Promise.reject(label + '须为 ' + value + ' 个字符' )
		return Promise.resolve()
	}}),
	num: (label='字符') => ({ validator: (_, v) => {
		if( window.$fn.isEmpty(v) ) return Promise.resolve()
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			return n < 0 ? Promise.reject(label + '不能为负数') : Promise.resolve()
		}
	}}),
	maxLen: (label='字符',value=2, mul) => ({ validator: (_, v) => {
		if( v ){
			if( mul && window.$fn.isString(v)) v = v.split(',')
			if( v.length > value ){
				let txt = Array.isArray(v) ? ('不能超过 ' + value + ' 个') : ('长度不能超过 ' + value + ' 个字符')
				return Promise.reject(label + txt )
			}else{
				return Promise.resolve()
			}
		}else{
			return Promise.resolve()
		}
	} }),
	minLen: (label='字符',value=2, mul) => ({ validator: (_, v) => {
		if( v ){
			if( mul && window.$fn.isString(v)) v = v.split(',')
			if( v.length < value ){
				let txt = Array.isArray(v) ? ('不能小于 ' + value + ' 个') : ('长度不能小于 ' + value + ' 个字符')
				return Promise.reject(label + txt )
			}else{
				return Promise.resolve()
			}
		}else{
			return Promise.resolve()
		}
	} }),
	rangeLen:(label='字符',value=[6,18]) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( v.length < value[0] ){
			return Promise.reject(label + '长度不能小于 ' + value[0] + '个字符')
		}else if( v.length > value[1] ){
			return Promise.reject(label + '长度不能大于 ' + value[1] + '个字符')
		}else{
			return Promise.resolve()
		}
	} }),
	max: (label, value) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( !Number.isInteger(v) ) return Promise.reject(label + '必须是整数')
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			if( v > value ){
				return Promise.reject(label + '最大值为 ' + value)
			}else{
				return Promise.resolve()
			}
		}
	} }),
	min: (label, value) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( !Number.isInteger(v) ) return Promise.reject(label + '必须是整数')
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			if( v < value ){
				return Promise.reject(label + '最小值为 ' + value)
			}else{
				return Promise.resolve()
			}
		}
	} }),
	range:(label,value=[0,10]) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( !Number.isInteger(v) ) return Promise.reject(label + '必须是整数')
		if( v < value[0] ){
			return Promise.reject(label + '最小值为 ' + value[0])
		}else if( v > value[1] ){
			return Promise.reject(label + '最大值为 ' + value[1])
		}else{
			return Promise.resolve()
		}
	} }),
	rangeFloat:(label,value=[0,10]) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( !Number.isInteger(v) ) return Promise.reject(label + '必须是整数')
		if( v < value[0] ){
			return Promise.reject(label + '最小值为 ' + value[0])
		}else if( v > value[1] ){
			return Promise.reject(label + '最大值为 ' + value[1])
		}else{
			return Promise.resolve()
		}
	} }),
	money:(label,value='1') => ({ validator: (_, v) => {
		let obj  = {
			'1': 10000,			// 5
			'10': 100000,		// 6
			'100': 1000000,		// 7
			'1000': 10000000, 	// 8
			'_1': 100000000, 	// 9
		}
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			const str = v.toString()
			const arr = str.split('.')
			if( arr.length === 2 && arr[1].length > 2 ) return Promise.reject(label + '最多保留 2 位小数')
			
			if(window.$fn.isArray(v)){
				if( v < value[0] ) return Promise.reject(label + '最小值为 ' + value[0])
				if( v >= value[1]  ) return Promise.reject(label + '不能超过 ' + value[1])
			}else{
				if(window.$fn.isNumber(value)){
					if( v < 0 ) return Promise.reject(label + '最小值为 0')
					if( v >= value ) return Promise.reject(label + '不能超过 ' + value)
				}else{
					if(obj[value]){
						if( v < 0 ) return Promise.reject(label + '最小值为 0')
						if( v >= obj[value]){
							let str = value + ' 万'
							if( value.startsWith('_') ){
								str = value.replace('_', '') + ' 亿'
							}
							return Promise.reject( label + '不能超过 ' + str )
						}
					}
				}
			}
			return Promise.resolve()
		}
	} }),
	date_range_all:(label) => ({ validator: (_, v) => {
		if(window.$fn.isEmpty( v )) return Promise.resolve()
		if( !v[0] ){
			return Promise.reject(`请选择${label}开始时间`)
		}
		if( !v[1] ){
			return Promise.reject(`请选择${label}结束时间`)
		}
		return Promise.resolve()
	} }),
	// 比例
	ratio: label => ({ validator: (_, v) => {
		if( window.$fn.isEmpty(v) ) return Promise.resolve()
		const n = window.$fn.isNumber(v) ? v : Number(v)
		if( isNaN( n ) ){
			return Promise.reject(label + '必须是数字')
		}else{
			if(v<0 || v >100) return Promise.reject(label + '须为 0 ~ 100 之间的数' )
			if( !Number.isInteger(v) ) return Promise.reject(label + '须为整数' )
			return Promise.resolve()
		}
	}}),
	len: (label, value) => ({ len:value, message: label + '长度为' + value + '位'}),
	phone: (label='手机号') => ({ pattern:/^1[3-9]\d{9}$/, message: label + '格式不正确' }),
	password: (label='密码') => ({ pattern:/^(?=.*[a-z])(?=.*\d)[^]{6,18}$/g, message: label + '是以字母头,且必须含用数字的 6-18 位字符' }),
	username: (label='用户名') => ({ pattern:/^[a-zA-Z0-9_-]{2,8}$/g, message: label + '是字母、数字、下划线(_)或连字符(-)组成的 2-8 位字符' }),
	email: (label='邮箱') => ({ pattern:/^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/, message: label + '格式不正确' }),
	bank_card: (label='银行卡') => ({ pattern:/^[0-9]{9,18}$/, message: label + '格式不正确' }),
	idcard: (label='身份证') => ({ pattern:/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: label + '格式不正确' }),
}

export default Index